Import mojo
Import diddy
Import model
Import multiChoicePanel

Class BonusCountingScreen Extends Screen
	Const START_GAME_STATE = 0
	Const BETWEEN_LEVEL_STATE = 1
	Const PLAYING_GAME_STATE = 2
	Const GAME_OVER_STATE = 3 
	Const CREATE_STATE = 4
	Const END_ROUND_STATE = 5
	Const START_ROUND_STATE = 6

	Const MULTI_CHOICE_PANEL_WIDTH:Int =150
	
	Const SCORE_STAR:Int = 100
	Const SCORE_TIME_LEFT:Int = 10
	
	Field objectsList:ArrayList<Sprite> = New ArrayList<Sprite>
	Field answerList:Int[] = New Int[20]
	Field nextAnswerList:Int[] = New Int[20]
	Field multiChoicePanel:MultiChoicePanel
	Field count:Int = 20
	Field answer:Int
	Field rowSpace:Int
	Field columnSpace:Int
	Field getReadyX:Float=0
	Field scoreStatusBar:ScoreStatusBar
	Field timer:Int = 0
	Field pointsToMax:Float = 12 ' Best if this is divisible by 3
	Field thingyAlpha:Float=1
	
	Field realMilliSecs1:Int
	Field realMilliSecs2:Int
	
	Field gameState:Int = CREATE_STATE
	Field nextGameState:Int = START_GAME_STATE
	
	Field getReadyTween1:Tween2= New Tween2(Tween2.Elastic.EaseInOut,SCREEN_WIDTH+100,SCREEN_WIDTH2,1000)
	Field getReadyTween2:Tween2= New Tween2(Tween2.Elastic.EaseInOut,SCREEN_WIDTH2,-100,1000)
	Field endLevelFadeTween:Tween2= New Tween2(Tween2.Cubic.EaseInOut,1,0,500)
	Field newLevelFadeTween:Tween2= New Tween2(Tween2.Cubic.EaseInOut,0,1,500)
	
	Field currentGetReadyTween:Int=0
	Field timeLastStateChanged:Int = 0
	Field timeSinceLastStateChange:Int = 0

	Field wrongRight:WrongRight = new WrongRight()
	
	Method New()
		name = "BonusCountingScreen"
		multiChoicePanel = New MultiChoicePanel(SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH,0, MULTI_CHOICE_PANEL_WIDTH,SCREEN_HEIGHT, 4)
		rowSpace = (SCREEN_HEIGHT)/4
		columnSpace = (SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH-50)/5
	End
	
	Method Start:Void()
		scoreStatusBar = New ScoreStatusBar(SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH-50, 10, SCREEN_HEIGHT-20, pointsToMax)

		timer = 60
		GenerateQuestion()
		answerList = nextAnswerList
		ChangeGameState(START_GAME_STATE)
	End
	
	Method GenerateQuestion:Void()
		answer=0
		objectsList.Clear()
		nextAnswerList = New Int[20]
		For Local ndx:Int = 0 to 19		
			nextAnswerList[ndx] = Rnd(2)
			if(nextAnswerList[ndx]=1)
				answer+=1
			End
			
		Next
		multiChoicePanel.NewQuestion(answer)	
		Print "Answer:"+answer
	End
	
	Method Render:Void()
		Local count:Int
		Cls
		
		SetAlpha(thingyAlpha)
		For Local row:Int = 0 to 3
			For Local column:Int = 0 to 4
				if(answerList[count]=1)
					DrawCircle(column*columnSpace+40, row*rowSpace+40,40)
				End
				
				count+=1
			Next
		Next
		SetAlpha(1)

			
		'
		' Render WrongRight
		'

	    wrongRight.Draw()								

		'
		' status bars
		'
		scoreStatusBar.Draw()
				
		'
		' Multiple Choice buttons
		'
		multiChoicePanel.Render()

		'
		' Status
		'
		DrawText ("TIME: "+timer, 0,10)
		
		'
		' Get ready message
		'
		if(gameState = BETWEEN_LEVEL_STATE)		
			_model.font.DrawText("GET READY!", getReadyX, SCREEN_HEIGHT2+30, AngelFont.ALIGN_CENTER)
		EndIf
		
		_model.messageWindow.Draw()
	End
	
	'
	' Update
	'
	Method Update:Void()
		Local isRight:Bool
		
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True)
			game.nextScreen = _model._introScreen			
		End
		
		'
		' Update state change, state changes are saved for the next update
		'
		UpdateState()
	
		'
		' Fade between rounds
		'
		if(gameState = END_ROUND_STATE)		
			endLevelFadeTween.Update()
			thingyAlpha=endLevelFadeTween.Value()
			if(endLevelFadeTween.isActive=false)
				ChangeGameState(START_ROUND_STATE)
			End
		elseif(gameState = START_ROUND_STATE)	
			newLevelFadeTween.Update()
			thingyAlpha=newLevelFadeTween.Value()	
			if(newLevelFadeTween.isActive=false)
				ChangeGameState(PLAYING_GAME_STATE)
			End
		endif

		'
		' Get ready message
		'
		if(gameState=BETWEEN_LEVEL_STATE)		
			if(currentGetReadyTween=1)
				getReadyTween1.Update()
				getReadyX=getReadyTween1.Value()
			elseif(currentGetReadyTween=2)
				getReadyTween2.Update()
				getReadyX=getReadyTween2.Value()
			End
			
			if(getReadyTween1.isActive=false and currentGetReadyTween=1)
				currentGetReadyTween=2
				getReadyTween2.Start()
			End
			
			if((newLevelFadeTween.isActive=false and TouchHit()) or timeSinceLastStateChange>3000)
				ChangeGameState(PLAYING_GAME_STATE)
			Endif
		EndIf
	
		'
		' Work out when 1 second has elapsed and update timer
		'
		if(gameState=PLAYING_GAME_STATE)
			realMilliSecs2=RealMillisecs()
			if(realMilliSecs2>realMilliSecs1+1000)
				timer-=1
				realMilliSecs1=RealMillisecs()
			endif
		endif
		
		'
		' status bars
		'
		scoreStatusBar.Update()
		
		'
		' Multiple Choice buttons
		'
		multiChoicePanel.Update()

		if(multiChoicePanel.pressedNumber<>MultiChoicePanel.NO_ANSWER)
			if(multiChoicePanel.pressedNumber=answer)
				'multiChoicePanel.NewQuestion(answer)	
				scoreStatusBar.AddPoints()
				
				ChangeGameState(END_ROUND_STATE)
				isRight=true
			Else 
				scoreStatusBar.SubtractPoints
				isRight=false
			EndIf
			
			wrongRight.Start((SCREEN_WIDTH-MULTI_CHOICE_PANEL_WIDTH)/2,SCREEN_HEIGHT2, isRight)
			
			multiChoicePanel.pressedNumber=MultiChoicePanel.NO_ANSWER
		End
				
		'
		' Check for game over
		'
		if (scoreStatusBar.GetSelectedStars() = scoreStatusBar.STARS_MAX)
			ChangeGameState(GAME_OVER_STATE)			
		elseif (timer<=0)
			ChangeGameState(GAME_OVER_STATE)
		endif
			
		'
		' Update WrongRights
		'
	    	wrongRight.Update()								

		
		'
		' Message Window
		'
		_model.messageWindow.Update()
	
	End
	
	
	Method UpdateState:void()
		if(nextGameState<>gameState)
			ChangeGameStateNextUpdate(nextGameState)
		End
		
		'
		' Time since state changed
		timeSinceLastStateChange = RealMillisecs()-timeLastStateChanged
	End
	
	Method ChangeGameState:void(newState:Int)
		nextGameState=newState
	End
	
	'***************************
	'
	'
	Method ChangeGameStateNextUpdate:void(newState:Int)
		Print "Changing state from "+gameState+"to : "+newState
		timeLastStateChanged=RealMillisecs()
		gameState=newState	
		Select(gameState)
			Case PLAYING_GAME_STATE
						
			Case BETWEEN_LEVEL_STATE	
				initBeginLevelState()
			Case START_GAME_STATE
				initStartState()
			Case GAME_OVER_STATE
				initGameOverState()
			Case END_ROUND_STATE
				initEndRoundState()
			Case START_ROUND_STATE	
				initStartRoundState()				
		End
	End
		'***************************
	'
	'
	Method initStartState:void()
		_model.messageWindow.Show("COUNT THE THINGYS" , New StartGameMessageCallback(self))
	End
	
	'***************************
	'
	'
	Method initBeginLevelState:void()
		newLevelFadeTween.Start()
		currentGetReadyTween=1
		getReadyTween1.Start()
	End
	
	
	'***************************
	'
	'
	Method initGameOverState:void()
		Local message:String
		Local score:Int
		
		score = scoreStatusBar.GetSelectedStars() * SCORE_STAR
		
		if(timer=0)
			message = "OUT OF TIME;;"
		else
			score += timer*SCORE_TIME_LEFT
			message = "LEVEL CLEARED;;"
		End
		message += "YOU GOT "+scoreStatusBar.GetSelectedStars()+" STARS;;"
		message += "SCORE: "+score
		
		_model.stars += scoreStatusBar.GetSelectedStars()
		_model.score += score
		_model.messageWindow.Show(message, new EndGameMessageCallback())
	End	
	
	'***************************
	'
	'
	Method initEndRoundState:void()
		endLevelFadeTween.Start()
		GenerateQuestion()
	End
		
	'***************************
	'
	'
	Method initStartRoundState:void()
		newLevelFadeTween.Start()
		currentGetReadyTween=1		
		
		'
		' Swap old answer for new one
		'
		answerList = nextAnswerList
		End
	
End

Class EndGameMessageCallback2 Implements WindowCallback

	Method onClose:Void()
		_model.bonusGameSuccess=true
		_model.bonusGameFailed=false
	'	game.screenFade.Start(50, True)
		game.nextScreen = _model._mazeScreen
	End
End

Class StartGameMessageCallback Implements WindowCallback
	Field bonusCountingScreen:BonusCountingScreen
	
	Method New(bonusCountingScreen:BonusCountingScreen)
		self.bonusCountingScreen = bonusCountingScreen
	End
	
	Method onClose:Void()
		self.bonusCountingScreen.ChangeGameState(bonusCountingScreen.BETWEEN_LEVEL_STATE)
	End
End